package dotdashdot.bradz.trendsettr;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/*
 * TODO:
 * 
 * Animation Set Trends //Doesnt look good
 * Outline Choices Set Trends //Doesnt look good
 * Fix Bug With See Trends Closing On Single Click
 * Make the Set Trend Layout a FrameLayout
 * Make a New Arrow Outline For The Set Trends Choices
 * Fix Bug With Double Connections When The User Hasn't Logged In //Done
 * 
 */

public class ActivityHome extends Activity {

	private ClassFonts fonts;
	private TextView title;
	private Bundle extras;
	//private ClassNetworkHelper helper;
	ClassNetworkHelper network;
	private ClassNetworkHelper helper;
	private ClassUserCredentials credentials;
	private ApplicationGlobalVars globals;
	private Button info_button, see_trends, start_trends, set_trends;
	private ClassUberLogger logger;
	
	Handler myHandler = new Handler() {

  	    @Override
  	    public void handleMessage(Message msg) {
  	        switch (msg.what) {
  	        case 0:
  	            break;
  	        case 5:
	        	format_credentials();
	        	break;
  	        default:
  	            break;
  	        }
  	    }
  	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		
		logger = new ClassUberLogger("ActivityHome");
		
		globals = ((ApplicationGlobalVars) this.getApplication());
		
		extras = getIntent().getExtras();
		fonts = new ClassFonts(this.getAssets());
		//info_button
		title = (TextView) this.findViewById(R.id.title_text);
		title.setTypeface(fonts.get_font("italic"));
		//db.reset_db();
		//network = new ClassNetworkHelper(this,((ApplicationGlobalVars) this.getApplication()));
		
		helper = new ClassNetworkHelper(this,((ApplicationGlobalVars) this.getApplication()));
		
		credentials = new ClassUserCredentials(globals);
		
		info_button = (Button) findViewById(R.id.info_button);
		set_trends = (Button) findViewById(R.id.set_trends);
		start_trends = (Button) findViewById(R.id.start_trends);
		see_trends = (Button) findViewById(R.id.see_trends);
		
		if(!credentials.logged_in && !helper.applying()){
			apply_for_credentials();
		}else{
			format_credentials();
		}
		create_buttons();
		logger.info("OnCreate Finished");
	}
	
	@Override
    public void onResume(){
    	super.onResume();
    	if(!credentials.logged_in && !helper.applying()){
    		apply_for_credentials();
		}else if(!helper.applying()){
			format_credentials();
		}
    	logger.info("OnResume Finished");
    }

	public void apply_for_credentials(){
		info_button.setEnabled(false);
		set_trends.setEnabled(false);
		start_trends.setEnabled(false);
		see_trends.setEnabled(false);
		helper.apply_for_credentials(credentials, myHandler);
		logger.info("Applying for Credentials");
	}
	
	private void format_credentials(){
		
		info_button.setEnabled(true);
		set_trends.setEnabled(true);
		start_trends.setEnabled(true);
		see_trends.setEnabled(true);
		
    	TextView user_name_text_view = (TextView) this.findViewById(R.id.user_name_footer);
    	TextView user_rank_text_view = (TextView) this.findViewById(R.id.user_rank_footer);
    	user_name_text_view.setTypeface(fonts.get_font("medium_italic"));
    	user_rank_text_view.setTypeface(fonts.get_font("medium"));
    	user_name_text_view.setText(credentials.name);
    	user_rank_text_view.setText("Trendsettr Rank: "+credentials.rank);
    	globals.set_credentials_store(credentials);
    	logger.info("Formatting Credentials...");
    	logger.info("Name: " + credentials.name);
    	logger.info("Email: " + credentials.email);
    	logger.info("Rank: " + credentials.rank);
    	
		/*ArrayList<NameValuePair> post_data = new ArrayList<NameValuePair>();
    	post_data.add(new BasicNameValuePair("trendName","Celtics"));
    	ClassNetworkHelper helper = new ClassNetworkHelper(this,globals);
    	helper.post_network(post_data, "/downvote",myHandler);*/
    }
	
	public void create_buttons(){
		
		info_button.setTypeface(fonts.get_font("demi_italic"));
		info_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	logger.info("Info button clicked");
            	Intent myIntent = new Intent(getBaseContext(), ActivityInfo.class);
        		startActivity(myIntent);
            }
        });
		set_trends.setTypeface(fonts.get_font("cond_medium"));
		set_trends.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	logger.info("Set trends clicked");
            	Intent myIntent = new Intent(getBaseContext(), ActivitySetTrend.class);
        		startActivity(myIntent);
            }
        });
		start_trends.setTypeface(fonts.get_font("cond_medium"));
		start_trends.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	logger.info("Start trends clicked");
            	//Intent myIntent = new Intent(getBaseContext(), ActivityStartTrend.class);
            	Intent myIntent = new Intent(getBaseContext(), ActivityCreateItemList.class);
            	startActivity(myIntent);
            }
        });
		see_trends.setTypeface(fonts.get_font("cond_medium"));
		see_trends.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	logger.info("See trends clicked");
            	Intent myIntent = new Intent(getBaseContext(), ActivityItemList.class);
        		startActivity(myIntent);
            }
        });
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}    

}
